package com.ego.member.po;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.util.Date;

import com.ego.common.constant.MemberConstant;
import com.ego.common.to.member.MemberTo;
import com.ego.common.vo.member.MemberVo;
import com.ego.member.vo.MemberRegisterVo;
import lombok.Data;

/**
 * @program: ego
 * @description 会员
 * @author KamTang
 * @date 2022-11-13 13:28:12
 */
@Data
@TableName("ums_member")
public class Member implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * id
	 */
	@TableId
	private Long id;
	/**
	 * 会员等级id
	 */
	private Long levelId;
	/**
	 * 用户名
	 */
	private String username;
	/**
	 * 密码
	 */
	private String password;
	/**
	 * 昵称
	 */
	private String nickname;
	/**
	 * 手机号码
	 */
	private String mobile;
	/**
	 * 邮箱
	 */
	private String email;
	/**
	 * 头像
	 */
	private String header;
	/**
	 * 性别
	 */
	private Integer gender;
	/**
	 * 生日
	 */
	private Date birth;
	/**
	 * 所在城市
	 */
	private String city;
	/**
	 * 职业
	 */
	private String job;
	/**
	 * 个性签名
	 */
	private String sign;
	/**
	 * 用户来源
	 */
	private Integer sourceType;
	/**
	 * 积分
	 */
	private Integer integration;
	/**
	 * 成长值
	 */
	private Integer growth;
	/**
	 * 启用状态
	 */
	private Integer status;
	/**
	 * 注册时间
	 */
	private Date createTime;
	/**
	 * 社交用户唯一id
	 */
	private String socialUid;

	/**
	 * 社交用户登录成功凭证
	 */
	private String accessToken;

	/**
	 * 凭证过期时间
	 */
	private Long expireTime;

	/**
	 * 购物车中的商品数量
	 */
	@TableField(exist = false)
	private Integer goodNumInCart;

	/**
	 * 收货地址
	 */
	@TableField(exist = false)
	private String defaultReceiveAddress;

	public void register(MemberRegisterVo register, String password) {
		this.setUsername(register.getUsername());
		this.setNickname(this.username);
		this.setMobile(register.getPhone());
		this.setLevelId(MemberConstant.DEFAULT_MEMBER_LEVEL);
		this.setPassword(password);
		this.setCreateTime(new Date());
		this.setStatus(1);
	}

	public void socialMerge(MemberTo socialInfo) {
		this.setSocialUid(socialInfo.getSocialUid());
		this.setAccessToken(socialInfo.getAccessToken());
		this.setExpireTime(socialInfo.getExpireTime());
	}

	public void autoRegister(JSONObject jsonObject) {
		this.setUsername(jsonObject.getString("name"));
		this.setNickname(this.username);
		this.setLevelId(MemberConstant.DEFAULT_MEMBER_LEVEL);
		this.setHeader(jsonObject.getString("profile_image_url"));
		this.setGender("m".equals(jsonObject.getString("gender")) ? 1 : 0);
		this.setCity(jsonObject.getString("location"));
		this.setCreateTime(new Date());
	}

	public MemberTo toTo() {
		return BeanUtil.copyProperties(this, MemberTo.class);
	}

	public MemberVo toVo() {
		return BeanUtil.copyProperties(this, MemberVo.class);
	}

	public static Member fromTo(MemberTo to) {
		return BeanUtil.copyProperties(to, Member.class);
	}
}
